<template>
	<view class="data-box">
		<view class="data-fixed-box">
			<!-- 搜索 -->
			<search @searchText="searchText" 
			    @openObjTypeList= "openObjTypeList"
				@searchAction="searchAction" 
				@inputSearchKw="inputSearchKw"
				@inputFocus="inputFocus"
				@setTitleValue="setTitleValue"
				:placeholder="placeholder"
				:total="total"
				:md="mdDj"
				ref="search">
			</search>
			<!-- 采购单据类型 -->
			<purchChangeBills ref="purchChangeBills" @setFilters="setFilters" v-show="!isSearching"></purchChangeBills>
			<!-- 过滤弹出 -->
			<purchfilters v-show="!isSearching" 
					@setST="setST" 
					@setHandlers="setHandlers"  
					@setStoreid="setStoreid"
					@setTime="setTime" 
					:filltersData="filltersData" 
					:orderData="orderData"
					ref="filter">
			</purchfilters>
			<searchTotal :total="total" v-show="isSearching"></searchTotal>
			<searchPop ref="searchPop" 
				:type="2"
				@changeObjType="changeObjType" 
				@showAndHideTypeList="showAndHideTypeList"
				@setDefaultObjType="setDefaultObjType">
			</searchPop>
			<searchKwResult ref="searchKwResult" @doSearch="doSearch"></searchKwResult>
		</view>
		<!-- 采购单据列表 -->
		<purchBillsList @setBillsInfoData="setBillsInfoData" 
				   @orderList="orderList"
				   @searchKwBillList="searchKwBillList"
				   ref="setBillsId"
				   :isSearching="isSearching"
				   :billsLs="billsLs" 
				   :hOffset="hOffset"
				   :st="orderData.st">
		</purchBillsList>
	</view>
</template>

<script>
	import search from "../comm/Search.vue";
	import purchChangeBills from "./PurchChangeBills.vue";
	import purchfilters from "./Purchfilters.vue";
	import purchBillsList from "./PurchBillsList.vue";
	import staticConst from "../../js/const/StaticConst.js";
	import config from "../../js/conf/Config.js";
	
	import eventCtrl from "../../js/event/EventCtrl.js";
	
	import searcher from "../../js/search/Searcher.js";
	
	import searchPop from "../comm/SearchPop.vue";
	
	import https from "../../js/https/Https.js";
	
	import searchKwResult from "../comm/SearchKwResult.vue";
	
	import searchTotal from "../comm/SearchTotal.vue";
	
	import accessCtrl from "../../js/limit/AccessCtrl.js";
	
	import localStore from "../../js/LocalStore.js"
	
	let pageNo = 0;
	let pageSize = staticConst.PAGE_SIZE;
	export default {
		components: { search , 
					  purchChangeBills , 
					  purchfilters , 
					  purchBillsList, 
					  searchPop, 
					  searchKwResult,
					  searchTotal
					},
		data() {
			return {
				orderData : {
					// ch : 1,//渠道可不传
					st : 2,//采购单据状态
					kw : '',//搜索关键词
					storeid : '',//仓库ID
					uid_cp : '',//经手人ID
					time : '',//时间类型
				},
				billsLs : [],
				filltersData : {
					ydBillsState : "全部",
					ydListState : "全部云店",
					billsState : "已确认",
					timeState : "全部时间",
					storesState : "全部仓库",
					handlersState : "全部经手人",
					statisticalIndex : 2,
					TimeStateIndex : '',
					StoresStateIndex : '',
					HandlersStateIndex : '',
				},
				placeholder:"搜索采购单据",
				isLoading:false,
				curPage : 1,//记录点击采购单据类型筛选的值
				isSearching :false,
				hOffset : 150,
				
				objType:null,
				keyWord:"",
				total:0,
				exactKw:null,
				mdDj:staticConst.CG_DJ,
				mySelf:null,
				
			};
		},
		
		methods: {
			async initData(id)
			{
				this.mySelf = await localStore.getUserInfo();
				this.clearPageLs();
				if(!id)
					this.orderList();
				else
				{
					this.setFilters(id);
					this.$refs.purchChangeBills.setCurId(id);
				}
				searcher.loadBillSchObjTypeLs(searcher.CH_CGO);
			},
			
			/*****搜索采购单据****/
			doSearch(item)
			{
				this.clearPageLs();
				let data = {};
				data.exact = 1;
				this.$refs.search.setCatSearchTxt(item.title);
				data.kw = item.title;
				this.exactKw = data;
				this.searchKwBillList();
			},
			
			/***设置输入框，选择的搜索关键字值****/
			setTitleValue(item)
			{
				this.$refs.search.setCatSearchTxt(item.title);
			},
			
			/****搜索采购单据****/
			searchKwBillList()
			{
				if(!this.exactKw)return;
				if(this.isLoading)return;
				this.isLoading = true;
				let that = this;
				let obj = {};
				if(this.objType)
					obj.objtype = this.objType.code;
				obj.md = staticConst.ORDER_CG;	
				obj.exact = this.exactKw.exact;
				obj.kw = this.exactKw.kw;
				obj.pfrom = pageNo * pageSize;
				obj.pnum = pageSize;
				https.searchKwBillList(obj,callBack);
				function callBack(data)
				{
					that.isLoading = false;
					if(data && data.status == 1)
					{
						if(!data.data) return;
						if(data.data.total)
							that.total = data.data.total;
						if(!data.data.ls)return;
						that.billsLs = that.billsLs.concat(data.data.ls);
						if(pageNo == 0)
						{
							if(that.billsLs.length > 0)
							{
								let orderData={};
								orderData.type = that.billsLs[0].type;
								orderData.odid = that.billsLs[0].id;
								that.$refs.setBillsId.setBillsId(that.billsLs[0].id);
								that.$emit("setBillsThree",orderData);
							}
						}
						pageNo ++;
					}
				}
			},
			
			/***输入关键字搜索*****/
			inputSearchKw(kw)
			{
				let data = {};
				let that = this;
				this.keyWord = kw;
				data.kw = kw;
				if(this.objType)
					data.objtype = this.objType.code;
				data.ch = searcher.CH_CGO;
			
				https.searchKwByObjType(data,callBack);
				function callBack(obj)
				{
					if(obj.status == 1)
					{
						that.$refs.searchKwResult.setKeyWord(that.keyWord);
						if(obj.data && obj.data.ls && obj.data.ls.length)
						{
							that.$refs.searchKwResult.setListData(obj.data.ls);
						}
						else
						{
							that.$refs.searchKwResult.setListData([]);
						}
					}
					else
					{
						that.$refs.searchKwResult.setListData([]);
					}
				}
			},
			
			/*****输入框获取焦点处理****/
			inputFocus()
			{
				this.$refs.searchPop.init();
				if(this.objType)
					this.placeholder = "请输入" + this.objType.name;
				this.$refs.search.searchAction(true);
			},
			
			/***弹出搜索类型列表***/
			openObjTypeList(value)
			{
				this.searchAction(true);
				if(value)
				{
					this.$refs.searchPop.show("show");
					if(this.objType)
						this.placeholder = "请输入" + this.objType.name;
				}
				else
				{
					this.$refs.searchPop.show();
				}
			},
			
			/****初始化设置默认objType*****/
			setDefaultObjType(item)
			{
				this.objType = item;
			},
			
			/***改变搜索类型***/
			changeObjType(item)
			{
				this.placeholder = "请输入" + item.name;
				this.objType = item;
				this.$refs.searchPop.show();
			},
			
			showAndHideTypeList(value)
			{
				this.$refs.search.setIsOpenList(value);
			},
			
			/*******/
			searchAction(value)
			{
				this.isSearching = value;
				this.clearPageLs();
				if(!value)
				{
					this.$refs.searchPop.show();
					this.exactKw = null;
					this.keyWord = "";
					this.total = 0,
					this.placeholder = "搜索采购单据";
					this.$refs.search.clearSearchTxt();
					this.$refs.searchKwResult.setListData([]);
					this.$refs.searchKwResult.setListData("");
					this.clearPageLs();
					this.orderList();
					this.hOffset = 150;
				}
				else
				{
					this.hOffset = 110;
				}
			},
			
			/*** 搜索 ***/
			searchText(kw)
			{
				/*****获取选择的关键字****/
				let item = this.getSelectedKwTitle();
				this.$refs.searchKwResult.setShowValue(false);
				this.clearPageLs();
				let data = {};
				if(item != null)
				{
					this.keyWord = item.title;
					data.exact = 1;
					this.$refs.search.setCatSearchTxt(item.title);
				}
				else
				{
					this.keyWord = kw;
					data.exact = 2;
				}
				data.kw = this.keyWord;
				this.exactKw = data;
				this.searchKwBillList();
			},
			
			/*****获取选择的关键字****/
			getSelectedKwTitle()
			{
				let item = this.$refs.searchKwResult.getSelectedItem();
				return item;
			},
			
			/****清除数据***/
			clearPageLs()
			{
				pageNo = 0;
				this.billsLs = [];
			},
			
			/*** 检测是否展示过滤栏 ***/
			setFilters(index)
			{
				this.clearPageLs();
				this.orderData = {
					st : 2,//采购单据状态
					kw : '',//搜索关键词
					shopid : '',//门店ID
					uid_cp : '',//经手人ID
					time : '',//时间类型
				};
				this.hOffset = 150;
				this.curPage = index;
				this.$refs.filter.resetFilters();
				this.orderData.st = index;
				this.orderList();
				this.filltersData = {
					ydBillsState : "全部",
					ydListState : "全部云店",
					billsState : "已确认",
					timeState : "全部时间",
					storesState : "全部仓库",
					handlersState : "全部经手人",
					statisticalIndex : 2,
					TimeStateIndex : '',
					StoresStateIndex : '',
					HandlersStateIndex : '',
				};
				uni.$emit(eventCtrl.ORDER_LIST_EVENT,index);
			},
			
			setBillsInfoData(res)
			{
				let orderData={};
				orderData.type = res.type;
				orderData.odid = res.id;
				this.$emit("setBillsThree",orderData);
				this.$refs.setBillsId.setBillsId(res.id);
			},
			
			/*** 设置订单筛选 ***/
			setST(res)
			{
				this.clearPageLs();
				this.orderData.st = res;
				
				console.log("st==============:",res);
				
				this.orderList();
			},
			
			/*** 设置时间删选 ***/
			setTime(res)
			{
				this.clearPageLs();
				this.orderData.time = res;
				this.orderList();
			},
			
			/*** 设置经手人筛选 ***/
			setHandlers(res)
			{
				this.clearPageLs();
				this.orderData.uid_cp = res;
				this.orderList();
			},
			
			/*** 设置门店筛选 ***/
			setStoreid(res)
			{
				this.clearPageLs();
				this.orderData.storeid = res;
				this.orderList();
			},
			
			/*** /采购单据列表  ORDER_LS ***/
			async orderList(index)
			{
				if(this.isLoading)return;
				this.isLoading = true;
				let url = config.CG_ORDER_LS;
				let param = {};
				let access = await accessCtrl.getPurchViewOtherBillLimit();
				if(!access)//没有查看他人采购单据权限，只能每次填上自己的id
				{
					this.orderData.uid_cp = this.mySelf.uid;
				}
				param = this.orderData;
				param.pfrom = pageNo * pageSize;
				param.pnum = pageSize;
				
				let res = await this.$api({url:url,data:param});
				this.isLoading = false;
				if(!res || !res.data) 
				{
					if(pageNo == 0)
					{
						this.$emit("setBillsThree");
					}
					return;
				}
				if( index == 'ss')
				{
					this.billsLs = res.data;
				}else
				{	
					this.billsLs = this.billsLs.concat(res.data);
				}
				if(this.billsLs.length > 0 && pageNo == 0)
				{
					let orderData={};
					orderData.type = this.billsLs[0].type;
					orderData.odid = this.billsLs[0].id;
					if(index == 1)
						orderData.yd = 1;
					this.$refs.setBillsId.setBillsId(this.billsLs[0].id);
					this.$emit("setBillsThree",orderData);
				}
				pageNo ++;
			},
			
			/***注册时间***/
			addEvent()
			{
				let that = this;
				uni.$on(eventCtrl.ACTION_ORDER_EVENT,function(data)
				{
					that.actionHandler(data);
				});
				uni.$on(eventCtrl.PAY_IN_AND_OUT_EVENT,function(data)
				{
					that.payInAndOut(data);
				});
			},
			
			/****清除事件****/
			removeEvent()
			{
				uni.$off(eventCtrl.ACTION_ORDER_EVENT);
				uni.$off(eventCtrl.PAY_IN_AND_OUT_EVENT);
			},
		
			/***收款/退款更新***/
			payInAndOut(data)
			{
				this.removeEvent();
				this.addEvent();
				let i,len = this.billsLs.length;
				for(i = 0; i<len; i++)
				{
					let obj = this.billsLs[i];
					if(obj.id == data.id)
					{
						// data.type == 1 --->  退款/收款    data.type == 2 ---> 退货申请
						if( data.type == 1 )
						{
							obj.status_act.status_pay_txt = data.status_pay_txt;
							obj.amount_notpay = data.amount_notpay;
						}
						else
						{
							obj.status_act.status_rt_txt = "退货申请已驳回";
						}
						
						console.log("ok");
						// this.billsLs[i] = obj;
						break;
					}
				}
			},
			
			/****处理操作菜单***/
			actionHandler(data)
			{
				this.removeEvent();
				this.addEvent();
				let i,len = this.billsLs.length;
				for(i = 0; i<len; i++)
				{
					let obj = this.billsLs[i];
					if(obj.id == data)
					{
						this.billsLs.splice(i,1);
						break;
					}
				}
				len = this.billsLs.length;
				if(len > 0)
				{
					let index = 0;
					if(len > 1)
					{
						if(i>1)
						{
							index = i - 1;
						}
						else
						{
							index = i;
						}
					}
					let obj = this.billsLs[index];
					if(obj)
					{
						this.setBillsInfoData(obj);
					}
					
				}
			},
			
			/****修改价格*****/
			changeYunBillPrice(data)
			{
				let len = this.billsLs.length;
				for(let i = 0; i<len; i++)
				{
					let temp = this.billsLs[i];
					if(temp.id == data.id)
					{
						temp.amount_all = data.amount_all;
						temp.amount_notpay = data.amount_notpay;
						break;
					}
				}
			},
			
			onClickPageHandler(event)
			{
				let detail = event.detail;
				if(detail.x < 72 || detail.x > 437 || detail.y < 70)
				{
					this.$refs.filter.onClickPageHandler(event);
				}
			}
		},
		
		mounted() {
			this.addEvent();
		},
	}
</script>
<style lang="scss">
.data-box
{
	position: fixed;
	margin-left: 0;
	box-sizing: border-box;
	width: 345px;
	height: 100vh;
	border-right: 1px solid #ECECEC;
	.data-fixed-box
	{	
		width: 345px;
		box-sizing: border-box;
		border-right: 1px solid #ECECEC;
		position: fixed;
		top: 0;
		background: #FFF;
		z-index: 1;
	}
}
</style>